package com.keyinong.park.personal;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.keyinong.park.R;
import com.keyinong.park.internermodel.ToolModel;
import com.keyinong.park.util.MyToast;
import com.loopj.android.http.JsonHttpResponseHandler;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class ModifyNameActivity extends Activity {
	private ImageButton img_title_back;
	private TextView tv_title_name;
	private TextView et_modName;
	private Button btn_modNameOk;
	SharedPreferences sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modifyname);
		initRes();
	}
	private void initRes() {
		img_title_back = (ImageButton) findViewById(R.id.img_title_back);
		tv_title_name = (TextView) findViewById(R.id.tv_title_name);
		et_modName = (TextView) findViewById(R.id.et_modName);
		btn_modNameOk = (Button) findViewById(R.id.btn_modNameOk);
		
		img_title_back.setVisibility(View.VISIBLE);
		tv_title_name.setText(getResources().getString(R.string.modifyName));
		
		img_title_back.setOnClickListener(modifyNameOnclick);
		btn_modNameOk.setOnClickListener(modifyNameOnclick);
	}
	
	private OnClickListener modifyNameOnclick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.img_title_back:
				finish();
				break;
			case R.id.btn_modNameOk:
				modifyName();
				break;

			default:
				break;
			}
		}
	};
	
	private void modifyName()
	{
		final String name=et_modName.getText().toString();
		 sp = getSharedPreferences("spAuthorization",
				Context.MODE_PRIVATE);
		String Authorization = sp.getString("Authorization", "");
		
		ToolModel toolModel=new ToolModel(ModifyNameActivity.this);
		toolModel.modifyName(Authorization, name, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					int status_code=response.getInt("status_code");
					String message=response.getString("message");
					if(status_code==200){
						MyToast.mtoast(ModifyNameActivity.this, message);
						Editor editor=sp.edit();
						editor.putString("user_name", name);
						editor.commit();
						finish();
					}else{
						MyToast.mtoast(ModifyNameActivity.this, message);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Throwable e,
					JSONObject errorResponse) {
				// TODO Auto-generated method stub
				super.onFailure(statusCode, e, errorResponse);
				try {
					String message=errorResponse.getString("message");
					MyToast.mtoast(ModifyNameActivity.this, message);
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		});
	}
}
